package br.com.itc.componentes.botoes
{
	import br.com.itc.componentes.util.FadeUtil;
	
	import flash.display.DisplayObject;
	import flash.events.MouseEvent;
	
	import mx.containers.TitleWindow;
	import mx.controls.Button;
	import mx.core.Application;
	import mx.core.IFlexDisplayObject;
	import mx.managers.PopUpManager;
	
	public class MenuBotao extends Button
	{
		private var _classe: Class;
		private var _icone: Class;
		private var _janela: TitleWindow;
		private var _ativarSegundoPlano: Boolean = true;
		private var _popUpCentralizado: Boolean = true;
		private var _isAlterarAlpha: Boolean = true;
		private var _isAbrirJanela: Boolean = true;
		
		public function get icone(): Class {
			return _icone;
		}
		
		public function set icone(icone: Class): void {
			this._icone = icone;
			this.setStyle("icon", _icone);
		}
		
		public function get janela(): TitleWindow {
			return _janela;
		}
		
		public function set janela(janela: TitleWindow): void { 
			_janela = janela
		}
		
		public function get classe(): Class {
			return _classe;
		}
		
		public function set classe(classe: Class): void {
			_classe = classe;
		}
		
		public function get ativarSegundoPlano(): Boolean {
			return _ativarSegundoPlano;
		}
		
		public function set ativarSegundoPlano(ativar: Boolean): void {
			this._ativarSegundoPlano = ativar;
		}
		
		public function get isPopUpCentralizado(): Boolean {
			return _popUpCentralizado;
		}
		
		public function set isPopUpCentralizado(centralizado: Boolean): void {
			this._popUpCentralizado = centralizado;
		}
		
		public function get isAlterarAlpha(): Boolean {
			return _isAlterarAlpha;
		}
		
		public function set isAlterarAlpha(alterarAlpha: Boolean): void {
			this._isAlterarAlpha = alterarAlpha;
		}
		
		public function get isAbrirJanela(): Boolean {
			return _isAbrirJanela;
		}
		
		public function set isAbrirJanela(abrirJanela: Boolean): void {
			this._isAbrirJanela = abrirJanela;
		}
		
		public function MenuBotao()
		{
			
			if (_isAlterarAlpha) {
				this.addEventListener(MouseEvent.MOUSE_OVER, alterarAlpha);
				this.addEventListener(MouseEvent.MOUSE_OUT, alterarAlpha);
			}
			
			if (_isAbrirJanela) {
				this.addEventListener(MouseEvent.CLICK, abrirJanela);
			}
			this.alpha = 0.8;
			this.width = 150; 
			this.labelPlacement = "bottom";
			this.buttonMode = true;
			this.useHandCursor = true;
			this.setStyle("cornerRadius", new Number(5));
			this.setStyle("fillColors", ["#FFFFFF", "#CCCCCC"]);
			this.setStyle("fontFamily", "Arial");
			this.setStyle("fontSize", new Number(10));
			this.setStyle("buttonWidth", new Number(100));
			this.setStyle("buttonHeight", new Number(50));
			this.setStyle("textAlign", "center");
		}
		
		private function alterarAlpha(evt: MouseEvent): void {
			var botao: MenuBotao = evt.target as MenuBotao;
			if (evt.type == MouseEvent.MOUSE_OVER) {
				FadeUtil.fadeAparecer(botao, 0.7, 1.0);
			} else {
				FadeUtil.fadeSumir(botao, 1.0, 0.7);
			}
		}
		
		private function abrirJanela(evt: MouseEvent): void {
			var aplicacao: DisplayObject;
			var ct: int = 0;
			while (ct < systemManager.rawChildren.numChildren) {
				var janela: DisplayObject = systemManager.rawChildren.getChildAt(ct);
				if (id == janela.name) {
					aplicacao = janela;
					break;
				}
				ct++;
			}
			if (aplicacao == null) {
				var popUp: IFlexDisplayObject = PopUpManager.createPopUp(Application.application as DisplayObject, classe, _ativarSegundoPlano);
				popUp.name = id;
				if (isPopUpCentralizado) {
					PopUpManager.centerPopUp(popUp);
				} else {
					PopUpManager.bringToFront(popUp);
				}
			}
		}
	}
}